package algorithm.linklist;

/**
 * @ Description：链表实现栈
 */
public class LinkStack {

    private Node node;
    private int size;

    // 出栈
    public int pop() {
        if (size < 1) {
            return -1;
        }
        int ans;
        ans = node.val;
        node = node.next;
        size--;
        return ans;
    }

    // 入栈
    public void push(int val) {
        Node cur = new Node(val);
        cur.next = node;
        node = cur;
        size++;
    }

    public void print() {
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkStack stack = new LinkStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
//        stack.print();
        System.out.println("stack.pop() = " + stack.pop());
        System.out.println("stack.pop() = " + stack.pop());
        System.out.println("stack.pop() = " + stack.pop());
        System.out.println("stack.pop() = " + stack.pop());
        System.out.println("stack.pop() = " + stack.pop());
    }

}
